const Koa = require('koa');
const fs = require('fs');
const path = require('path');
const app = new Koa();

app.use(async ctx => {
    if(ctx.request.url=='/'){
        const indexHtmlPath = path.resolve(__dirname, 'index.html');
        console.log('indexHtmlPath: ', indexHtmlPath);
        const indexHtml = await fs.promises.readFile(indexHtmlPath);
        ctx.body = indexHtml;
        ctx.response.set('Content-Type', 'text/html');
    }else if(ctx.request.url=='/main.js'){
        const mainJsPath = path.resolve(__dirname, 'main.js');
        console.log('mainJsPath: ', mainJsPath);
        const mainJs = await fs.promises.readFile(mainJsPath);
        ctx.body = mainJs;
        ctx.response.set('Content-Type', 'application/javascript');
    }else if(ctx.request.url=='/App.vue'){
        // AST 语法通过App.createElement() ->构建原生dom
        const appVuePath = path.resolve(__dirname, 'App.vue');
        console.log('appVuePath: ', appVuePath);
        const appVue = await fs.promises.readFile(appVuePath);
        ctx.body = appVue;
        ctx.response.set('Content-Type', 'application/javascript');
    }else{
        ctx.body = 'Hello World';
    }
});

app.listen(4000,()=>{
    console.log('http://localhost:4000/')
});